package com.lujianfei.filesend.ui.search

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Build
import android.view.MenuItem
import android.view.View
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableList.OnListChangedCallback
import com.lujianfei.common.AlbumHelper
import com.lujianfei.filesend.MyApplication
import com.lujianfei.filesend.R
import com.lujianfei.filesend.base.BaseActivity
import com.lujianfei.filesend.base.toast
import com.lujianfei.filesend.databinding.ActivitySearchDeviceBinding
import com.lujianfei.filesend.ui.dialog.DialogButton
import com.lujianfei.filesend.ui.dialog.DialogUtils
import com.lujianfei.filesend.ui.search.adapter.SearchDeviceItem
import com.lujianfei.filesend.ui.search.viewmodel.SearchDeviceViewModel
import com.lujianfei.filesend.ui.server.viewmodel.ServerViewModel
import com.lujianfei.filetransfer.FileTransferManager
import com.lujianfei.filetransfer.IPScanner
import com.lujianfei.filetransfer.ScanStatus
import com.lujianfei.filetransfer.TcpManager
import com.lujianfei.filetransfer.TcpMessageListener


/**
 * Author: lujianfei
 * Date: 2023/10/16 15:55
 * Description: 查找设备
 */

class SearchDeviceActivity: BaseActivity<ActivitySearchDeviceBinding>(), TcpMessageListener {

    companion object {
        fun start(context: Context?) {
            context?.startActivity(Intent(context, SearchDeviceActivity::class.java))
        }
    }

    private val mAlbumHelper by lazy { AlbumHelper() }

    private val viewModel: SearchDeviceViewModel by lazy { buildViewModel() }
    override fun resourceId() = R.layout.activity_search_device

    override fun initView() {
        TcpManager.instance.addListener(this)
        binding?.vm = viewModel
    }

    override fun initData() {
        ClientConnectDialog.start(supportFragmentManager,
           onConfirm = {
             viewModel.showLoading()
             TcpManager.instance.connect(serviceNo = it)
        }, onCancel = {
            finish()
        })
    }

    override fun initEvent() {
        binding?.onBackClick = View.OnClickListener {
            onBackPressed()
        }
        binding?.widgetInputLayout?.onTextSend = {
            viewModel.sendMessage(it)
        }
        binding?.widgetInputLayout?.onPhotoClick = {
            mAlbumHelper.openAlbum(this@SearchDeviceActivity) {
                FileTransferManager.instance.sendFile(MyApplication.instance, it, address = TcpManager.instance.targetAddress, port = TcpManager.port)
            }
        }
        viewModel.onScrollToEnd = {
            binding?.recyclerview?.layoutManager?.scrollToPosition(0)
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if(item.itemId == android.R.id.home){
            onBackPressed()
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onDestroy() {
        TcpManager.instance.stopClient()
        TcpManager.instance.removeListener(this)
        super.onDestroy()
    }


    override fun onBackPressed() {
        DialogUtils.show(this, content = "确认退出当前连接?", confirm = DialogButton("确认", onClickListener = {
            super.onBackPressed()
        }), cancel = DialogButton("取消", onClickListener = {

        }))
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        mAlbumHelper.onActivityResult(requestCode, resultCode, data)
    }

    override fun onServerStart(address: String, port: Int) {
    }

    override fun onClientAccept(address: String) {
    }

    override fun onClientClose(address: String) {
    }

    override fun onMessageReceive(ip: String, message: String) {
        viewModel.onMessageReceive(ip, message)
    }

    override fun onNameAccept(address: String, name: String) {
    }

    override fun onConnectFailed(address: String, error: String) {
        viewModel.hideLoading()
        runOnUiThread {
            toast("连接失败 $error")
            finish()
        }
    }

    override fun onConnectSuccess(address: String) {
        viewModel.hideLoading()
        runOnUiThread {
            toast("连接成功")
        }
        val name = "${Build.MANUFACTURER} ${Build.BRAND} ${Build.MODEL}"
        TcpManager.instance.sendName(name)
    }
}